'use strict';
const {product,Ob} = require('../model/use.model')

let products = [
    {
        id: 1,
        productName: 'iPhone 13 Pro 256G 1',
        price: 8399,
        stockNum: 500,
        supplier: 'Apple'
    },
    {
        id: 2,
        productName: 'iPhone 13 Pro 256G 2',
        price: 8399,
        stockNum: 500,
        supplier: 'Apple'
    },
    {
        id: 3,
        productName: 'iPhone 13 Pro 256G 3',
        price: 8399,
        stockNum: 500,
        supplier: 'Apple'
    },
    {
        id: 4,
        productName: 'iPhone 13 Pro 256G 4',
        price: 8399,
        stockNum: 500,
        supplier: 'Apple'
    },
    {
        id: 5,
        productName: 'iPhone 13 Pro 256G 5',
        price: 8399,
        stockNum: 500,
        supplier: 'Apple'
    },
    {
        id: 6,
        productName: 'iPhone 13 Pro 256G 6',
        price: 8399,
        stockNum: 500,
        supplier: 'Apple'
    },
    {
        id: 7,
        productName: 'iPhone 13 Pro 256G 7',
        price: 8399,
        stockNum: 500,
        supplier: 'Apple'
    },
]

// 显示数据
let fn_list = (ctx, next) => {
    let list = product.findAll()
    ctx.body = list
}

// 查找数据
let fn_getbyid = (ctx, next) => {
    let id = ctx.request.params.id

    let product = products.filter(item => {
        return item.id == id
    })
    if (product.length > 0) {
        ctx.body = {
            code: 1002,
            data: product[0],
            msg: '获取商品成功'
        }
    } else {
        ctx.body = {
            code: 1002,
            data: '',
            msg: '获取商品失败'
        }
    }

}

// 添加数据
let fn_post = (ctx, next) => {
    let id = ctx.request.params.body
    let product = ctx.request.body;

    if (products.length !== 0) {
        product.id = products[products.length - 1].id + 1
    } else {
        product.id = 1
    }

    console.log(product);
    products.push(product)
    // console.log(products);
    ctx.body = products
}

// 修改数据
let fn_put = (ctx, next) => {
    let id = ctx.request.params.id
    let obj = ctx.request.body
    let product = products.filter(item => {
        return item.id == id
    })

    if (product.length > 0) {
        let temobj = product[0]
        temobj.productName = obj.productName,
        temobj.price = obj.price,
        temobj.stockNum = obj.stockNum,
        temobj.supplier = obj.supplier
        temobj.remark = obj.remark

        ctx.body = {
            code: 9001,
            data: temobj,
            msg: '修改成功!'
        }
    } else {
        ctx.body = {
            code: 10005,
            data: '',
            msg: '未找到该商品'
        }
    }
}

// 删除数据
let fn_delete = (ctx, next) => {
    let id = ctx.request.params.id
    console.log(`id:` + id);
    // let product = products.filter(item=>{
    //     return item.id == id
    // })
    console.log("长度：" + products.length);

    for (let i = 0; i < products.length; i++) {
        if (products[i].id == id) {
            products.splice(i, 1)
        }
    }
    console.log(products);
    ctx.body = {
        code: '10001',
        data: '',
        msg: '删除成功!'
    }
}

// restful api
module.exports = {
    'get /product': fn_list,
    'get /product/:id': fn_getbyid,
    'post /product': fn_post,
    'put /product/:id': fn_put,
    'delete /product/:id': fn_delete
}